
package cwb1;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Calendar;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class AchievementRegistry {

        //The parent key for all achievements in the database.
        private Key allAchievements = KeyFactory.createKey(Achievement.staticGetKind(), "allAchievements");
                
        //design pattern singleton.     
        private static AchievementRegistry instance = new AchievementRegistry();
                
        /**
         * There is no possibility of creating an object of this class.
         */
        private AchievementRegistry()
        {
        }
        
        /**
         * This method returns the only possible operational instance of the AchievementRegistry class.
         * 
         * @return The only possible operational instance of the AchievementRegistry class.
         */
        public static AchievementRegistry getInstance()
        {
        	return instance;
        }
        
        public void add(Achievement achievement)
        {               
            Entity entity = new Entity(achievement.getKind(), achievement.getId(), allAchievements);
            DatabaseCommunicator.getInstance().put(entity,achievement.getProperties());
        }
        
        public Achievement get(String achievementId) throws EntityNotFoundException {
            Key achievementKey = KeyFactory.createKey(allAchievements, Achievement.staticGetKind(), achievementId);
            return new Achievement(DatabaseCommunicator.getInstance().get(achievementKey).getProperties());
        }
        
        public List<Achievement> getAll() {
            ArrayList<Achievement> achievements = new ArrayList<Achievement>();
            for(Entity lusEntity : DatabaseCommunicator.getInstance().getAll(allAchievements, Achievement.staticGetKind())) {
            	achievements.add(new Achievement(lusEntity.getProperties()));
            }
            return achievements;
        }        
        
        /**
         * 
         * @return
         */
        public List<Achievement> getActiveAchievements() {
                
        	ArrayList<Achievement> activeAchievements = new ArrayList<Achievement>();
        	Date currentDate = Calendar.getInstance().getTime();
        	for(Entity lusEntity : DatabaseCommunicator.getInstance().getAll(allAchievements, Achievement.staticGetKind())) {
            	Achievement temp = new Achievement(lusEntity.getProperties());
            	if(currentDate.before(temp.getStopDate()))
            	{
            		activeAchievements.add(temp); 
            	}
        	}
        	
            return activeAchievements;
        }
        
        /**
    	 * Removes the the given achievement.
    	 * 
    	 * @param achievement The achievement that has to be removed.
    	 */
    	public void delete(Achievement achievement)
    	{
    		Key achievementKey = KeyFactory.createKey(allAchievements, achievement.getKind(), achievement.getId());
    		DatabaseCommunicator.getInstance().delete(achievementKey);
    	}
    	
    	public void update(Achievement achievement) throws EntityNotFoundException {
    		Key achievementKey = KeyFactory.createKey(allAchievements, achievement.getKind(), achievement.getId());
    		DatabaseCommunicator.getInstance().put((DatabaseCommunicator.getInstance().get(achievementKey)), achievement.getProperties());
    	}  
        
}
